package oa.hleast.xswl.controller.oauth.zlb;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.constant.AuthConstant;
import oa.hleast.common.utils.JsonUtil;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.common.config.AppProps;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@AllArgsConstructor
public class ZLBAuth {

    private final AppProps appProps;

    /**
     * 票据换取令牌
     */
    public String ticketValidation(String type, String ticket) {

        String projectId = "";
        String projectSecret = "";
        if (AuthConstant.MP_ZJFWW_APP.equals(type)) {
            projectId = appProps.getWlhServiceCode();
            projectSecret = appProps.getWlhServicePwd();
        } else if (AuthConstant.MP_ZJFWW_WLYY_APP.equals(type)) {
            projectId = appProps.getWlyyServiceCode();
            projectSecret = appProps.getWlyyServicePwd();
        } else {
            throw new BizException("系统错误");
        }

        // 从Request请求的参数中获取ssotoken
        String url = "https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth?method=ticketValidation&";
        String time = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        log.info("time = {}", time);
        String sign = MD5.create().digestHex(projectId + projectSecret + time);
        url = url + "servicecode=" + projectId + "&time=" + time + "&sign=" + sign + "&st=" + ticket + "&datatype=json";

        log.info("url = {}", url);
        String result = HttpUtil.get(url);
        log.info("响应数据 = {}", result);
        Map<String, Object> map = JsonUtil.string2Collection(result, new TypeReference<Map<String, Object>>() {
        });
        Object token = map.get("token");
        if (ObjectUtils.isEmpty(token)) {
            throw new BizException("票据认证失败");
        }
        return token.toString();
    }

    /**
     * 通过令牌获取详细用户信息
     */
    public String getUserInfo(String type, String ssotoken) {
        String projectId = "";
        String projectSecret = "";
        if (AuthConstant.MP_ZJFWW_APP.equals(type)) {
            projectId = appProps.getWlhServiceCode();
            projectSecret = appProps.getWlhServicePwd();
        } else if (AuthConstant.MP_ZJFWW_WLYY_APP.equals(type)) {
            projectId = appProps.getWlyyServiceCode();
            projectSecret = appProps.getWlyyServicePwd();
        } else {
            throw new BizException("系统错误");
        }

        String url = "https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth?method=getUserInfo&";
        String time = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        log.info("time = {}", time);
        String sign = MD5.create().digestHex(projectId + projectSecret + time);
        url = url + "servicecode=" + projectId + "&time=" + time + "&sign=" + sign + "&token=" + ssotoken + "&datatype=json";

        log.info("url = {}", url);
        String result = HttpUtil.get(url);
        log.info("响应数据 = {}", result);
        return result;
    }

}
